package com.android.questions.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.android.base.util.ActivityClickUtil;
import com.android.base.util.DisplayUtil;
import com.android.questions.R;
import com.android.questions.databinding.DialogAnswerFailureBinding;
import com.android.questions.util.AnimUtil;
import com.dq.ad.AdManager;
import com.dq.ad.UMManager;

/**
 * FileName: DialogAnswerFailure
 * Author: admin
 * Date: 2021/11/8 9:39
 * Description:
 */
public class DialogAnswerFailure extends Dialog implements View.OnClickListener {

    private DialogAnswerFailureBinding failureBinding;
    private final Activity activity;
    private OnClickLister onClickLister;

    public DialogAnswerFailure(@NonNull Activity a) {
        super(a, R.style.dialog_loading);
        activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        failureBinding = DialogAnswerFailureBinding.inflate(activity.getLayoutInflater());
        setContentView(failureBinding.getRoot());

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity);
        lp.height = DisplayUtil.getScreenHeight(activity);
        getWindow().setAttributes(lp);

        getWindow().setWindowAnimations(R.style.dialog_answer_correct);

        setCancelable(false);
        setCanceledOnTouchOutside(false);
        initData();
        initLister();
    }

    @SuppressLint("SetTextI18n")
    private void initData() {
        String answerSum = "<font color='#FBFF41'>复活</font>后此次不扣除<font color='#FBFF41'>体力</font>";
        failureBinding.tvContent.setText(Html.fromHtml(answerSum));

        AnimUtil.receiveAnim(activity, failureBinding.ivResurrection);

        AdManager.getInstance().showNativeAD(activity, failureBinding.nativeContainer, DisplayUtil.getScreenWidthDp(activity) - 48, 0, AdManager.NATIVE_AD_TYPE_UP_IMAGE, false, new AdManager.OnNativeVideoADListener() {
            @Override
            public void onSuccess() {
                failureBinding.nativeContainer.setBackgroundColor(activity.getResources().getColor(R.color.Color_FFFFFF));
            }

            @Override
            public void onClosed() {
                failureBinding.nativeContainer.setBackgroundColor(activity.getResources().getColor(android.R.color.transparent));
            }

            @Override
            public void onError() {

            }

            @Override
            public void onClick() {

            }

            @Override
            public void onShow(View view) {

            }
        });
    }

    private void initLister() {
        failureBinding.ivResurrection.setOnClickListener(this);
        failureBinding.ivClose.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (ActivityClickUtil.getInstance().isFastClick()) {
            //防止用户快速点击
            return;
        }
        dismiss();
        if (v.getId() == R.id.iv_resurrection) {
            if (onClickLister != null) {
                UMManager.onEvent(UMManager.Event.resurrection_click);
                onClickLister.onResurrection();
            }
        } else if (v.getId() == R.id.iv_close) {
            if (onClickLister != null) {
                onClickLister.onNext();
            }
        }
    }

    public DialogAnswerFailure setOnClickLister(OnClickLister clickLister) {
        this.onClickLister = clickLister;
        return this;
    }

    public interface OnClickLister {
        void onNext();

        void onResurrection();
    }
}
